using System;
using System.Collections.Generic;
using System.Text;
using BodySoundSimulator.CaseData;
using System.IO;
using System.Reflection;

namespace BodySoundSimulator.CommonElements
{
    class SimulatorFileManager
    {
        public static readonly string CasesFolderName = "Cases";

        public static string GetSoundFileName(string caseID,
            AuscultationSiteInfo selectedSite,
            PatientView view,
            AuscultationConfiguration configuration)
        {
            return string.Format("case_{0}_{1}_{2}_{3}_{4}.wav",
                caseID, 
                view.ToString(),
                selectedSite.Location.X,
                selectedSite.Location.Y,
                configuration.ToString());
        }

        public static string GetMapFileName(string caseID)
        {
            return string.Format("case_{0}.xml", caseID);
        }

        public static string GetCaseFolderName(string caseID)
        {
            return string.Format("case_{0}", caseID);
        }

        public static string GetInfoFormatString(string fileName)
        {
            Assembly executingAssembly = Assembly.GetExecutingAssembly();
            TextReader reader = new StreamReader(executingAssembly.GetManifestResourceStream(string.Format("BodySoundSimulator.Recording.{0}", fileName)));
            string content = reader.ReadToEnd();
            return content;
        }

        public static string GetCaseFolderPath(string caseID)
        {
            return Path.Combine(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), CasesFolderName), GetCaseFolderName(caseID));
        }

        internal static void RemoveCaseInformation(string caseID)
        {
            string folderName = GetCaseFolderPath(caseID);
            if (Directory.Exists(folderName))
            {
                Directory.Delete(folderName, true);
            }
        }

        internal static Dictionary<string, CaseInformation> GetExistingCaseInformation()
        {
            Dictionary<string, CaseInformation> output = new Dictionary<string, CaseInformation>();

            string folderName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), 
                CasesFolderName);
            if (Directory.Exists(folderName))
            {
                //load case information
                string[] folders = Directory.GetDirectories(folderName);

                foreach (string name in folders)
                {
                    //extract case information from folder
                    string caseInformationXmlName = Path.Combine(name, string.Format("{0}.xml", Path.GetFileName(name)));
                    CaseInformation caseInfo = CaseInformation.XmlDeserialize(caseInformationXmlName);
                    if (File.Exists(caseInformationXmlName))
                    {
                        output.Add(caseInfo.CaseID, caseInfo);
                    }
                }
            }
            return output;
        }

		internal static string GetSoundFilePath(string caseID,
			AuscultationSiteInfo selectedSite,
			PatientView view,
			AuscultationConfiguration configuration)
		{
			return Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
					Path.Combine(Path.Combine(SimulatorFileManager.CasesFolderName, SimulatorFileManager.GetCaseFolderName(caseID)), SimulatorFileManager.GetSoundFileName(caseID, selectedSite, view, configuration)));
		}
    }
}
